from django.shortcuts import redirect, render, HttpResponse, reverse
from django.http import JsonResponse
from django.shortcuts import HttpResponse
from django_redis import get_redis_connection
from chart_web import models
from chart_web.form import poets


def transition(request, username):
    '''创建/进入房间'''
    # 传送过来的带组名(123...)
    # 如果有返回聊天室
    # 如果没有返回错误信息
    if request.method == 'GET':
        return render(request, 'transition.html')
    form = poets.poet_password(request, data=request.POST)
    if form.is_valid():
        group = form.cleaned_data.get('group')
        url = reverse('poet', kwargs={'group': group})
        return redirect(url)
    else:
        return render(request, 'transition.html', {'error': form.errors})


def poet(request, group):
    '''聊天室视图'''
    return render(request, 'poet.html', locals())
